package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.UserBankInfo;
import cn.wolfcode.p2p.business.service.IUserBankInfoService;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.UserContext;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BankController {
        @Autowired
        private IUserBankInfoService userBankInfoService;
        @Autowired
        private IUserInfoService userInfoService;

        @RequestMapping("bankInfo")
        public String bankInfo(Model model){
                UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());
                if(userInfo.getBankInfoId()==null){
                        model.addAttribute("userinfo",userInfo);
                        return "bankInfo";
                }else{
                        UserBankInfo bankInfo = userBankInfoService.selectByid(userInfo.getBankInfoId());
                        model.addAttribute("bankInfo",bankInfo);
                        return "bankInfo_result";
                }
        }
        @RequestMapping("bankInfo_save")
        @ResponseBody
        public JSONResult bankInfoSave(UserBankInfo userBankInfo){
                JSONResult jsonResult = new JSONResult();
                try {
                        userBankInfoService.save(userBankInfo);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

}
